package com.example.netty.util;

import com.example.netty.pojo.HybWarning;
import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import lombok.Data;

import javax.lang.model.element.Modifier;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.time.Instant;

public class AnnotationGenerator {

    public static void main(String[] args) {
        generateAnnotationClass("com.example.netty.pojo", "HybWarning", HybWarning.class);
    }

    private static <T> void generateAnnotationClass(String packageName, String className, Class<T> clazz) {
        TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className)
                .addModifiers(Modifier.PUBLIC)
                .addSuperinterface(Serializable.class);


        // 获取类中所有字段
        Field[] fields = clazz.getDeclaredFields();

        Annotation[] classAnnotations = clazz.getAnnotations();
        for (Annotation annotation : classAnnotations) {
            classBuilder.addAnnotation(AnnotationSpec.get(annotation));
        }
        classBuilder.addAnnotation(AnnotationSpec.builder(Data.class).build());
        if(!clazz.isAnnotationPresent(Measurement.class)){
            classBuilder.addAnnotation(AnnotationSpec.builder(Measurement.class).addMember("name", "$S", className).build());
        }

        for (Field field : fields) {
            String fieldName = field.getName();
            if(fieldName.equals("id")){
                continue;
            }



            // 获取字段上的所有注解
            Annotation[] annotations = field.getAnnotations();
            FieldSpec.Builder fieldSpecBuilder;
            // 构建 FieldSpec 对象，添加字段类型和字段名
            if (fieldName.equals("serialVersionUID")) {
              fieldSpecBuilder  = FieldSpec.builder(long.class, fieldName,
                        Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).initializer("$LL", 1L);
            }else if(fieldName.equals("createTime")){
                fieldSpecBuilder = FieldSpec.builder(Instant.class, fieldName, Modifier.PRIVATE);
            }else{
                fieldSpecBuilder = FieldSpec.builder(String.class, fieldName, Modifier.PRIVATE);
            }


            // 复制原有字段上的注解
            for (Annotation annotation : annotations) {
                fieldSpecBuilder.addAnnotation(AnnotationSpec.get(annotation));
            }

            // 新增加 @Compare 注解
            if(!fieldName.equals("serialVersionUID")){
                if(fieldName.equals("createTime") && !field.isAnnotationPresent(Column.class)){
                    fieldSpecBuilder.addAnnotation(AnnotationSpec.builder(Column.class)
                            .addMember("name", "$S", "_time")
                            .addMember("timestamp", "$L", true).build());
                }else if(!field.isAnnotationPresent(Column.class)){
                    fieldSpecBuilder.addAnnotation(AnnotationSpec.builder(Column.class).build());
                }

            }


            // 构建 FieldSpec 对象
            FieldSpec generatedField = fieldSpecBuilder.build();

            // 将 FieldSpec 对象添加到类中
            classBuilder.addField(generatedField);
        }

        TypeSpec myClass = classBuilder.build();

        JavaFile javaFile = JavaFile.builder(packageName, myClass)
                .build();
//        System.out.println("Current Working Directory: " + System.getProperty("user.dir"));
        File file = new File(System.getProperty("user.dir")+"/src/main/java");

        try {
            javaFile.writeTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}





